package com.rh.gtoa.common.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.rh.gtoa.common.service.FileManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.InetAddress;

@Slf4j
@Service
public class FileManageServiceImpl implements FileManageService {

    @Value("${nginxFilePath}")
    private String nginxFilePath;
    @Value("${nginxFilePathForDownload}")
    private String nginxFilePathForDownload;
    /*    @Value("${fileHostPath}")
    private String fileHostPath;*/
    /*    @Override
    public String upload(MultipartFile file,String modelName) {
        File test = new File(fileHostPath+modelName+ "/"+ IdUtil.simpleUUID() +"-"+file.getOriginalFilename());
        if (!test.exists()){
            test.mkdirs();
        }
        try {
            file.transferTo(test);
        }catch (IOException e){
            log.error(file.getOriginalFilename()+"文件上传失败", e);
        }
        log.info(test.getPath());
        return test.getPath();
    }

    @Override
    public String download(String fileName, HttpServletResponse response){
        String rawFileName="";
        */
    /*fileName=fileHostPath+fileName;*/
    /*
        File file = new File(fileName);
        if (!file.exists()){
            return "文件不存在";
        }
        byte[] bytes = new byte[1000];
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);

            response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
            String[] split = fileName.split("-");

            if (split != null) {
                for (int i=1;i<split.length;i++){
                    rawFileName=rawFileName+split[i]+"-";
                }
            }
            rawFileName=StrUtil.sub(rawFileName,0,-1);
            log.info(rawFileName);

            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(rawFileName ,"UTF-8"));
            response.setContentType("application/octet-stream; charset=UTF-8");

            outputStream = response.getOutputStream();
            int length;
            while ((length = bufferedInputStream.read(bytes)) != -1){
                outputStream.write(bytes, 0, length);
            }
            outputStream.flush();
        }catch (Exception e){
            log.error("文件下载失败", e);
        }finally {
            try {
                if (bufferedInputStream != null){
                    bufferedInputStream.close();
                }

                if (outputStream != null){
                    outputStream.close();
                }

                if (fileInputStream != null){
                    fileInputStream.close();
                }
            }catch (IOException e){
                log.error(e.getMessage(), e);
            }
        }
        return rawFileName;
    }

    @Override
    public String showImage(String fileName, HttpServletResponse response) {
        try {
            FileInputStream fis=new FileInputStream(fileName);
            int i=fis.available();
            byte[] data=new byte[i];
            fis.read(data);
            fis.close();
            response.setContentType("image/*");
            ServletOutputStream out=response.getOutputStream();
            out.write(data);
            out.close();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }*/

    @Override
    public String uploadToNginxForOpen(MultipartFile file, String modelName)  {
        try {
            String path=modelName+ "/"+ IdUtil.simpleUUID() +"-"+file.getOriginalFilename();
            File test = new File(nginxFilePath+path);
            if (!test.exists()){
                test.mkdirs();
            }
            file.transferTo(test);
            InetAddress address = InetAddress.getLocalHost();
            String ip=address.getHostAddress();
            String prefix = "/"+StrUtil.subSuf(nginxFilePath,3);
            String finalPath="http://"+ip+prefix+path;
            return finalPath;
        }catch (Exception e){
            log.error(file.getOriginalFilename()+"文件上传失败", e);
            return file.getOriginalFilename()+"文件上传失败";
        }
    }
    
    @Override
    public String uploadToNginxForDownload(MultipartFile file, String modelName) {
        try {
            String path=modelName+ "/"+ IdUtil.simpleUUID() +"-"+file.getOriginalFilename();
            File test = new File(nginxFilePathForDownload+path);
            if (!test.exists()){
                test.mkdirs();
            }
            file.transferTo(test);
            InetAddress address = InetAddress.getLocalHost();
            String ip=address.getHostAddress();
            String prefix = "/"+StrUtil.subSuf(nginxFilePathForDownload,3);
            String finalPath="http://"+ip+prefix+path;
            return finalPath;
        }catch (Exception e){
            log.error(file.getOriginalFilename()+"文件上传失败", e);
            return file.getOriginalFilename()+"文件上传失败";
        }
    }
}
